package com.leantech.android.browser.ui.setting

import android.os.Bundle
import android.text.style.AbsoluteSizeSpan
import androidx.core.text.buildSpannedString
import androidx.core.widget.doOnTextChanged
import com.leantech.android.browser.R
import com.leantech.android.browser.databinding.ActivityFeedbackBinding
import com.leantech.android.browser.ui.BaseActivity
import com.leantech.android.browser.utils.onThrottleClick
import com.leantech.android.browser.utils.toast
import com.cscj.android.utils.SpanUtils

class FeedbackActivity : BaseActivity() {

    private lateinit var binding: ActivityFeedbackBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityFeedbackBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
    }

    private fun initView() {
        binding.topBar.setTitle(R.string.title_feedback)
        binding.topBar.addLeftBackImageButton().onThrottleClick {
            finish()
        }

        binding.content.doOnTextChanged { _, _, _, count ->
            updateUI()
        }

        binding.editWechat.doOnTextChanged { text, start, before, count ->
            updateUI()
        }

        binding.feedbackHint.text = buildSpannedString {
            append("您的联系方式有助于我们沟通解决问题")
            append(SpanUtils.makeSpan("（仅工作人员可见）", AbsoluteSizeSpan(14, true)))
        }

        updateUI()

        binding.btnSubmit.onThrottleClick {
            toast("感谢您的反馈~", onDismiss = {
                finish()
            })
        }
    }

    private fun updateUI() {
        val contentLength = binding.content.text?.length ?: 0
        binding.inputHint.text = "$contentLength/500"

        val wechatLength = binding.editWechat.text?.length ?: 0
        val enabled = contentLength > 0 && wechatLength > 0
        binding.btnSubmit.isEnabled = enabled
    }
}